﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Rizmar
{
    public partial class PesquisaSacadoForm : Rizmar.PesquisaBaseForm
    {
        private Form parent;

        public PesquisaSacadoForm()
        {
            InitializeComponent();
            textBoxBusca.Focus();
        }

        public PesquisaSacadoForm(LancamentoForm lanc)
        {
            InitializeComponent();
            parent = lanc;
            textBoxBusca.Focus();
        }

        public PesquisaSacadoForm(SacadoForm sacado)
        {
            InitializeComponent();
            textBoxBusca.Focus();
            parent = sacado;
        }

        private void PesquisaSacadoForm_Load(object sender, EventArgs e)
        {
            CNPJradioButton.Checked = true;
            this.sacadoTableAdapter.Fill(this.rizmarDataSet.Sacado);
            textBoxBusca.Focus();

        }

        private void textBoxBusca_TextChanged(object sender, EventArgs e)
        {
            RizmarDataSet.SacadoDataTable cdt = new RizmarDataSet.SacadoDataTable();
            if (radioButtonNome.Checked)
                cdt = sacadoTableAdapter.PesquisaPorNome(textBoxBusca.Text);
            else
            {
                if (CPFRadioButton.Checked)
                {
                    if (textBoxBusca.Text.Length == 3 || textBoxBusca.Text.Length == 7)
                        textBoxBusca.AppendText(".");
                    if (textBoxBusca.Text.Length == 11)
                        textBoxBusca.AppendText("-");
                }
                else
                {
                    if (textBoxBusca.Text.Length == 2 || textBoxBusca.Text.Length == 6)
                        textBoxBusca.AppendText(".");
                    if (textBoxBusca.Text.Length == 10)
                        textBoxBusca.AppendText("/");
                    if (textBoxBusca.Text.Length == 15)
                        textBoxBusca.AppendText("-");
                }
                cdt = sacadoTableAdapter.PesquisaPorDocumento(textBoxBusca.Text);
            }
            sacadoBindingSource.DataSource = cdt;
        }

        private void radioButtonDocumento_CheckedChanged(object sender, EventArgs e)
        {
            textBoxBusca.Clear();
            RizmarDataSet.SacadoDataTable cdt = new RizmarDataSet.SacadoDataTable();
            if (radioButtonNome.Checked)
                cdt = sacadoTableAdapter.PesquisaPorNome(textBoxBusca.Text);
            else
                cdt = sacadoTableAdapter.PesquisaPorDocumento(textBoxBusca.Text);
            sacadoBindingSource.DataSource = cdt;
            textBoxBusca.Focus();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {

            if (dataGridView.SelectedRows.Count > 0)
            {
                if (parent is LancamentoForm)
                {
                    (parent as LancamentoForm).titulos[(parent as LancamentoForm).n_titulos - 1].sacadoTextBox.Text = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
                    (parent as LancamentoForm).titulos[(parent as LancamentoForm).n_titulos - 1].codigoSacadoLabel.Text = dataGridView.SelectedRows[0].Cells[0].Value.ToString();
                    (parent as LancamentoForm).titulos[(parent as LancamentoForm).n_titulos - 1].documentoTextBox.Text = dataGridView.SelectedRows[0].Cells[2].Value.ToString();
                    
                    if (dataGridView.SelectedRows[0].Cells[2].Value.ToString().Contains("/"))
                        (parent as LancamentoForm).titulos[(parent as LancamentoForm).n_titulos - 1].CNPJradioButton.Checked = true;
                    else
                        (parent as LancamentoForm).titulos[(parent as LancamentoForm).n_titulos - 1].CPFradioButton.Checked = true;
                    (parent as LancamentoForm).titulos[(parent as LancamentoForm).n_titulos - 1].setDividaSacadoLabel(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value.ToString()));
                }
                if (parent is SacadoForm)
                {
                    (parent as SacadoForm).idTextBox.Text = dataGridView.SelectedRows[0].Cells[0].Value.ToString();
                    (parent as SacadoForm).nomeTextBox.Text = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
                    (parent as SacadoForm).documentoTextBox.Text = dataGridView.SelectedRows[0].Cells[2].Value.ToString();
                    (parent as SacadoForm).emailTextBox.Text = dataGridView.SelectedRows[0].Cells[3].Value.ToString();
                    (parent as SacadoForm).logradouroTextBox.Text = dataGridView.SelectedRows[0].Cells[6].Value.ToString();
                    (parent as SacadoForm).numeroTextBox.Text = dataGridView.SelectedRows[0].Cells[7].Value.ToString();
                    (parent as SacadoForm).bairroTextBox.Text = dataGridView.SelectedRows[0].Cells[8].Value.ToString();
                    (parent as SacadoForm).cEPTextBox.Text = dataGridView.SelectedRows[0].Cells[9].Value.ToString();
                    (parent as SacadoForm).cidadeTextBox.Text = dataGridView.SelectedRows[0].Cells[10].Value.ToString();
                    (parent as SacadoForm).estadoTextBox.Text = dataGridView.SelectedRows[0].Cells[11].Value.ToString();
                    (parent as SacadoForm).obsTextBox.Text = dataGridView.SelectedRows[0].Cells[12].Value.ToString();
                    (parent as SacadoForm).complementoTextBox.Text = dataGridView.SelectedRows[0].Cells[13].Value.ToString();
                    (parent as SacadoForm).setStatus(SacadoForm.StatusSacadoCadastro.editando);
                    (parent as SacadoForm).idTextBox.Enabled = false;
                }
                this.Close();
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void dataGridView_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.Equals(Convert.ToChar(Keys.Enter)))
                buttonOK_Click(sender, e);
        }

        private void textBoxBusca_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.Equals(Convert.ToChar(Keys.Back)))
            {
                if (textBoxBusca.Text.EndsWith(".") || textBoxBusca.Text.EndsWith("-") || textBoxBusca.Text.EndsWith("/"))
                {
                    e.Handled = true;
                    textBoxBusca.Text = textBoxBusca.Text.Remove(textBoxBusca.Text.Length - 1);
                    textBoxBusca.Text = textBoxBusca.Text.Remove(textBoxBusca.Text.Length - 2);
                    textBoxBusca.Select(textBoxBusca.Text.Length, 0);
                }

            }
            if (char.IsDigit(e.KeyChar))
            {
                if (CPFRadioButton.Checked)
                {
                    if (textBoxBusca.Text.Length == 14)
                        e.Handled = true;
                }
                if (CNPJradioButton.Checked)
                {
                    if (textBoxBusca.Text.Length == 18)
                        e.Handled = true;
                }
            }
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                if (!radioButtonNome.Checked)
                e.Handled = true;
            if (e.KeyChar == '\r')
            {
                if (dataGridView.SelectedRows.Count == 1)
                {
                    buttonOK_Click(sender, e);
                    return;
                }
                if (CPFRadioButton.Checked)
                {
                    if (textBoxBusca.Text.Length == 14)
                    {
                        if (dataGridView.SelectedRows.Count == 0)
                        {
                            SacadoForm novo = new SacadoForm(parent as LancamentoForm);
                            novo.CPFRadioButton.Checked = CPFRadioButton.Checked;
                            novo.documentoTextBox.Text = textBoxBusca.Text;
                            novo.ShowDialog();
                            System.Windows.Forms.SendKeys.Send("{TAB}");
                            System.Windows.Forms.SendKeys.Send("{TAB}");
                            System.Windows.Forms.SendKeys.Send("{TAB}");
                            System.Windows.Forms.SendKeys.Send("{TAB}");
                            if (novo.DialogResult == DialogResult.OK)
                                this.Close();
                        }
                        else
                            buttonOK_Click(sender, e);
                    }
                }
                if (CNPJradioButton.Checked)
                {
                    if (textBoxBusca.Text.Length == 18)
                    {
                        if (dataGridView.SelectedRows.Count == 0)
                        {
                            SacadoForm novo = new SacadoForm(parent as LancamentoForm);
                            novo.CPFRadioButton.Checked = CPFRadioButton.Checked;
                            novo.documentoTextBox.Text = textBoxBusca.Text;
                            novo.ShowDialog();
                            if (novo.DialogResult == DialogResult.OK)
                                this.Close();
                        }
                        else
                            buttonOK_Click(sender, e);
                    }
                }
                if (radioButtonNome.Checked)
                    if (dataGridView.SelectedRows.Count == 0)
                    {
                        SacadoForm novo = new SacadoForm(parent as LancamentoForm);
                        novo.nomeTextBox.Text = textBoxBusca.Text;
                        novo.ShowDialog();
                        if (novo.DialogResult == DialogResult.OK)
                            this.Close();
                    }
                    else
                        buttonOK_Click(sender, e);
            }
        }

        private void novoButton_Click(object sender, EventArgs e)
        {
            SacadoForm novo = new SacadoForm(parent as LancamentoForm);
            novo.documentoTextBox.Text = textBoxBusca.Text;
            novo.CPFRadioButton.Checked = CPFRadioButton.Checked;
            novo.ShowDialog();
            if (novo.DialogResult == DialogResult.OK)
                this.Close();
            System.Windows.Forms.SendKeys.Send("{TAB}");
        }

       
    }
}
